<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <p>

    </p>
    <div>

    </div>
    <script>
        var p = document.querySelector("p");
        //获取当前时间

        function time() {
            var date = new Date();
            var y = date.getFullYear()
            var m = date.getMonth() + 1;
            var d = date.getDate();
            var day = date.getDay();
            var h = date.getHours();
            var min = date.getMinutes();
            var s = date.getSeconds();
            // console.log(`${y}年${m}月${d}日 星期${day} ${h}:${min}:${s}`);
            p.innerHTML = `${y}年${m}月${d}日 周${day} ${h}:${min}:${s}`;
        }
        time()

        //定时器  定时执行一段代码
        setInterval(time, 1000);





        //距离中秋节倒计时

        var div = document.querySelector("div")
        function time2() {
            var date1 = new Date("2022-9-10").getTime();
            var date2 = new Date().getTime();
            var cha = date1 - date2;
            var y = parseInt(cha / 1000 / 60 / 60 / 24 / 30 / 12);
            var m = parseInt(cha / 1000 / 60 / 60 / 24 / 30 % 12);
            var d = parseInt(cha / 1000 / 60 / 60 / 24 % 30);
            var h = parseInt(cha / 1000 / 60 / 60 % 24);
            var min = parseInt(cha / 1000 / 60 % 60);
            var s = parseInt(cha / 1000 % 60);
            div.innerHTML = `距中秋还有 ${y}年${m}月${d}日 ${h}:${min}:${s}`;

        }
        time(2);
        setInterval(time2, 1000);
    </script>
</body>

</html>